package com.handpay.zztong.hp.main.widget;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.bean.Rate;
import com.handpay.zztong.hp.bean.Rates;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.ui.ZZTShowDialog;

import java.util.ArrayList;

/**
 * Created by lyang on 2017/4/5.
 */

public class RateDialogFragment extends DialogFragment implements View.OnClickListener,RadioGroup.OnCheckedChangeListener{
    private static final String KEY_TITLE="title";
    private static final String KEY_BEAN="rates";
    private static final String KEY_CANCELABLE="cancelable";
    private Rates mrates=null;
    private Rate current_rate=null;
    private RadioGroup rg_root=null;
    private OnRateViewButtonLinsener linsener=null;

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if(mrates!=null){
            HPLog.i(HPLog.TAG_RESULT,"---onCheckedChanged--checkedId:"+checkedId);
            for(Rate obj:mrates.getRates()){
                if(checkedId==obj.getRateCodeId()){
                    current_rate=null;
                    current_rate=obj;
                    break;
                }
            }
        }
    }

    public interface OnRateViewButtonLinsener{
        /**
         * 按钮点击事件处理
         * @param dialog
         * @param obj
         */
        void onOkClick(Dialog dialog,Rate obj);
    }

    public static RateDialogFragment newInstance(String title, Rates rates, boolean cancelable,OnRateViewButtonLinsener mlinsener){
        RateDialogFragment instance = new RateDialogFragment();
        instance.linsener=mlinsener;
        Bundle args = new Bundle();
        args.putString(KEY_TITLE,title);
        args.putSerializable(KEY_BEAN,rates);
        args.putBoolean(KEY_CANCELABLE,cancelable);
        instance.setArguments(args);
        return instance;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View root=getActivity().getLayoutInflater().inflate(R.layout.hp_mulitrate_new,null);
        rg_root=(RadioGroup)root.findViewById(R.id.rg_root);
        rg_root.setOnCheckedChangeListener(this);
        TextView rateview_title=(TextView)root.findViewById(R.id.rateview_title);
        Button btn_confirmRate=(Button)root.findViewById(R.id.btn_confirmRate);
        btn_confirmRate.setOnClickListener(this);
        ZZTShowDialog dialog = new ZZTShowDialog(getActivity(),root, R.style.dialog);
        Bundle args=getArguments();
        if(args!=null){
            String title=args.getString(KEY_TITLE);
            rateview_title.setText(title);
            mrates=(Rates)args.getSerializable(KEY_BEAN);
            boolean cancelable =args.getBoolean(KEY_CANCELABLE);
            bundListData(mrates);
            dialog.getDialog().setCancelable(cancelable);
        }else{
            HPLog.i(HPLog.TAG_RESULT,"savedInstanceState==null");
        }
        return dialog.getDialog();
    }

    //绑定数据
    private void bundListData(Rates rates){
        if(rates!=null){
            ArrayList<Rate> datas= rates.getRates();
            HPLog.i(HPLog.TAG_RESULT,"rate data count:"+datas.size());
            for(Rate obj:datas){
                addView(obj);
            }
        }
    }

    //根据数据动态追加View
    private void addView(Rate obj){
        RadioButton rb_child=new RadioButton(getActivity());
        LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(50,30,40,30);
        rb_child.setId(obj.getRateCodeId());
        rb_child.setTag(obj.getRateCodeId());
        rb_child.setText(obj.getRateName());
        rb_child.setChecked(obj.getIsDefault()!=null&&obj.getIsDefault().equals("1")?true:false);
        rb_child.setTextColor(Color.BLACK);
        rb_child.setButtonDrawable(R.drawable.rateview_radiobutton);
        rb_child.setPadding(50,10,10,10);
        rb_child.setLayoutParams(lp);
        rg_root.addView(rb_child);
    }


    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.btn_confirmRate){
            if(linsener!=null){
                linsener.onOkClick(getDialog(),current_rate);
            }
        }
    }
    //在onDismiss之后
    @Override
    public void onDetach() {
        super.onDetach();
        if(rg_root!=null){
            rg_root.removeAllViews();
            rg_root=null;
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        mrates=null;
        current_rate=null;
        linsener=null;
    }
}
